/*
 * @Author: 缄默
 * @Date: 2021-12-12 16:45:26
 * @LastEditors: 缄默
 * @LastEditTime: 2021-12-30 17:38:14
 */

//状态模式

#include <iostream>
#include <vector>

using namespace std;

class State {
protected:
    string _str;

public:
    virtual void Handle() = 0;

    virtual ~State() { }

    void setStr(string& str) {
        _str = str;
    }

    State() : _str("default") { }

    State(string& str) : _str(str) { }
};

class Context {
private:
    State* _state;

public:
    Context() { }

    void setState(State* state) {
        _state = state;
    }

    State* getState() {
        return _state;
    }

    void Handle() {
        _state->Handle();
    }
};






class ConcreteState1 : public State {
public:

    ConcreteState1(string& str) : State(str) { }
    void Handle() {
        cout << _str << endl;
    }
};


class ConcreteState2 : public State {
public:

    ConcreteState2() {
    }
    void Handle() {
        cout << "ConcreteState2 is called..." << endl;

    }
};


int main() {
    Context* con = new Context();
    string s1 = "state1";
    State* sta1 = new ConcreteState1(s1);
    State* sta2 = new ConcreteState2();

    con->setState(sta1);
    con->Handle();

    con->setState(sta2);
    con->Handle();

    delete con;
    delete sta1;
    delete sta2;
    return 0;
}
